<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <script src="/layui/layui.js"></script>
    <script src="/js/jquery/jquery-3.5.1.js"></script>
    <script src="/js/book/book.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/bookList.css">
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
    <script src="/component/pear/pear.js"></script>
    <script src="/js/book/bookCommon.js"></script>
    <title>数据管理</title>
</head>
<body>

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">来源</label>
                <div class="layui-input-inline">
                    <select name="source" xm-select="select1">
                        <option value="1" >北京</option>
                        <option value="2" >上海</option>
                        <option value="3" >广州</option>
                        <option value="4" >深圳</option>
                        <option value="5">天津</option>
                    </select>
                </div>
                <label class="layui-form-label">书籍</label>
                <div class="layui-input-inline">
                    <input type="text" name="bookInfo" placeholder="搜索书名或作者" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>

            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" class="layui-table" lay-filter="user-table">
        </table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script src="/component/pear/pear.js"></script>

<script>
    var ds = {};
    var chapterInfo="";
    layui.use(['layer', 'form','jquery','table'], function() {

        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let layer = layui.layer;

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }else if (obj.event === 'bklikn'){
                active.bklikn(obj);
            }else if(obj.event ==='lastLink'){
                active.lastLink(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });
        form.on('submit(user-query)', function(data) {
            $.ajax({
                type:"post",
                url:'/BookAdmin/queryBookInfo',
                data:JSON.stringify(data.field),
                dataType:"json",
                async:false,
                contentType : "application/json; charset=utf-8",
                success: function(res) {
                    if(res.data){
                        debugger
                        table.render({
                            elem:"#user-table",
                            data:res.data,
                            skin: 'line',
                            toolbar: '#user-toolbar',
                            defaultToolbar: [{
                                layEvent: 'refresh',
                                icon: 'layui-icon-refresh',
                            }, 'filter', 'print', 'exports'],
                             cols: [
                            [
                                {type: 'checkbox'}
                                , {field: 'bookSourceName', title: '图书来源', width: 180}
                                , {field: 'bookName', title: '书名', width: 180, templet: bookLink }
                                , {field: 'bookAuthor', title: '作者', width: 180}
                                , {field: 'bookKind', title: '类别', width: 180}
                                , {field: 'bookDuce', title: '简介', width: 180}
                                , {field: 'bookState', title: '连载状态', width: 180}
                                , {field: 'bookLastChapter', title: '最新章节', width: 180,templet: lastLink}

                            ] //设置表头
                        ],
                            parseData: function(data) { //res 即为原始返回的数据
                                return {
                                    "code": 0, //解析接口状态
                                    "msg": "", //解析提示文本
                                    "count": 100, //解析数据长度
                                    "data": data //解析数据列表
                                }
                            }
                            ,limit:10
                            , limits: [10, 50, 100]
                            , page: true //开启分页
                        });
                    }else{
                        layer.msg("未获取数据")
                    }
                }
            });

           return false;
        });

        form.on('switch(user-enable)', function(obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        window.add = function() {
            drawer.open({
                direction: "top",
                dom: ".layer-top",
                distance: "30%"
            });
        }

        window.edit = function(obj) {
            layer.msg("修改");
        }

        window.remove = function(obj) {
            layer.confirm('确定要删除该用户', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['userId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function(result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].userId + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function(result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }

        //监听行单击事件（双击事件为：rowDouble）
        table.on('rowDouble(user-table)', function(obj){
            var data = obj.data;
            ds = data;
            //iframe窗
            var index = layer.open({
                title:data.bookAuthor+'：'+data.bookName,
                type: 2,
                content: '/forward/book/admin/bookChapterList',
                data:ds,
                area: ['100%', '100%'],
                maxmin: false
            });
            layer.full(index);
        });
        function bookLink(d){
            var addLink = d.bookName;
            if ('' == addLink || null == addLink || undefined == addLink) {
                return '';
            }
            if (addLink.length > 0) {
                   return '<a class="layui-table-link" href="javascript:void(0);"  lay-event="bklikn">' + d.bookName + '</a>';
            }

        }
        function lastLink(d){
            var lastLink = d.bookName;
            if ('' == lastLink || null == lastLink || undefined == lastLink) {
                return '';
            }
            if (lastLink.length > 0) {
                return '<a class="layui-table-link" href="javascript:void(0);"  lay-event="lastLink">' + d.bookLastChapter + '</a>';
            }

        }
        var active = {
            bklikn:function (obj){
                var data = obj.data;
                ds = data;
                //iframe窗
                var index = layer.open({
                    title:data.bookAuthor+'：'+data.bookName,
                    type: 2,
                    content: '/forward/book/admin/bookChapterList',
                    data:ds,
                    area: ['100%', '100%'],
                    maxmin: false
                });
                layer.full(index);

            },
            lastLink : function (obj) {
                var data = obj.data;
                $.ajax({
                    type:"post",
                    url:'/BookAdmin/queryChapter',
                    data:JSON.stringify(data),
                    dataType:"json",
                    async:false,
                    contentType : "application/json; charset=utf-8",
                    success: function(res) {
                        debugger
                        chapterInfo =res.data;
                        layer.open({
                            title:chapterInfo.chapterName,
                            type: 2,
                            content: '/forward/book/admin/bookRead',
                            data:chapterInfo,
                            area: ['70%', '95%'],
                            shadeClose: true,
                            maxmin: false
                        });
                    }
                })
            }
        }
    })
</script>
</body>
</html>